﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Configuration;

namespace ListenToTwitter {
    public partial class RunViaTwitter : ServiceBase {
        public RunViaTwitter() {
            InitializeComponent();
        }

        protected override void OnStart(string[] args) {
            rvtWork worker = new rvtWork(GetTwitterAccountInfo);
            System.Threading.Thread th = new System.Threading.Thread(worker.Listen);
            th.Start();
        }

        protected override void OnStop() {
        }


        private AccountInfo GetTwitterAccountInfo{
            get {
                //ToDo: Don't forget to change the config file to YOUR twitter account.
                AccountInfo ai = new AccountInfo();
                try {
                    ai.User = ConfigurationSettings.AppSettings["twitter_Username"].ToString();
                    ai.Pass = ConfigurationSettings.AppSettings["twitter_Username"].ToString();
                    ai.Frequency = int.Parse(ConfigurationSettings.AppSettings["twitter_Frequency"].ToString());
                    string msg = string.Format("User: {0}, Pass:{1}, Freq:{2}", ai.User, ai.Pass, ai.Frequency);
                    EventLog.WriteEntry(msg, EventLogEntryType.Error);
                } catch (Exception err) {
                    EventLog.WriteEntry("Failed reading twitter account info.", EventLogEntryType.Error);
                    ai.Frequency = 10;
                    ai.User = "BillyJoe";
                    ai.Pass = "Jimbob";
                }

                return ai;
            }
        }

        public struct AccountInfo {
            public string User;
            public string Pass;
            public int Frequency;
        }
    }
}
